﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace NielsRask.K242.Core
{
	public class Sector
	{
		SizeF size;
		Random rnd;
		public List<Asteroid> Asteroids
		{
			get;
			private set;
		}
		public List<Fleet> Fleets
		{
			get;
			private set;
		}

		public List<AI.Emperor> Players { get; private set; }

		public SizeF Size
		{
			get { return size; }
		}

		public Sector(SizeF size)
		{
			this.size = size;
			rnd = new Random();
			Asteroids = new List<Asteroid>();
			Fleets = new List<Fleet>();
			Players = new List<AI.Emperor>();
		}

		public PointF GetRandomPoint()
		{
			return new PointF(size.Width * (float)rnd.NextDouble(), size.Height * (float)rnd.NextDouble());
		}

		public void Populate(int asteroidCount)
		{
			//tjek om den er indenfor 5-10% af banestørrelsen fra enum anden asteroide?
			for (int i = 0; i < asteroidCount; i++)
				Asteroids.Add(new Asteroid(GetRandomPoint()));
		}

		public void Update()
		{
			foreach (AI.Emperor emp in Players)
				emp.Update();	// det er vist denne der updater colony og asteroid? asteroid skal være sector direkte ..

			UpdateFleets();
		}
		public void UpdateFleets()
		{
			foreach (Fleet fleet in Fleets)
			{
				fleet.UpdatePosition();
				foreach (var asteroid in GetAsteroidsWithinRange(fleet.Position, fleet.SensorRange))
				{
					if (asteroid.EmperorKnowledge.ContainsKey(fleet.Emperor) == false)	// den er ikke kendt før!
						fleet.OnAsteroidDiscovered(asteroid);
				}
			}
		}

		public List<Asteroid> GetAsteroidsWithinRange(PointF point, float range)
		{
			List<Asteroid> list = new List<Asteroid>();
			foreach (Asteroid asteroid in Asteroids)
				if (GetDistance(point, asteroid.Position) <= range)
					list.Add(asteroid);

			return list;
		}

		public List<Fleet> GetFleetsWithinRange(PointF point, float range)
		{
			List<Fleet> list = new List<Fleet>();
			foreach (Fleet fleet in Fleets)
				if (GetDistance(point, fleet.Position) <= range)
					list.Add(fleet);

			return list;
		}

		private float GetDistance(PointF p1, PointF p2)
		{
			return (float)Math.Sqrt(Math.Pow(p1.X - p2.X, 2) + Math.Pow(p1.Y - p2.Y, 2));
		}

		public static void ResolveCombat(Fleet attacker, Colony defender)
		{
			Random rnd = new Random();
			while (attacker.Ships.Count > 0 && defender.GetDefenseBuildings().Count > 0)
			{
				var targetlist = defender.GetDefenseBuildings();
				foreach (Ship ship in attacker.Ships)	// skip denne hvis kolonien har early warning
				{
					var target = targetlist[rnd.Next(targetlist.Count)];
					target.TakeDamage(ship.FirePower);
				}

				foreach (Building b in targetlist)
				{
					var target = attacker.Ships[rnd.Next(attacker.Ships.Count)];
					target.TakeDamage(b.FirePower);
				}
			}
			if (attacker.Ships.Count > 0)
			{
				defender.Emperor = attacker.General.Emperor;
				defender.Emperor.Colonies.Add(defender);
			}
		}
	}
}
